I think demon possession is superstitious rubbish thought up by ancient people who were unable to explain mental illnesses. Remember, there was a time when people did not know that the brain is responsible for our thoughts and actions. They thought that there was a separate inanimate being inhabiting the body - a "ghost in the machine", if you will.
So when presented with mental people who were having auditory hallucinations and split personality disorders, and behaving violently or irrationally, they would naturally think that there must be another "ghost in the machine" - that the person's body is possessed.
However, by the time science finally figured out the brain's role in thought, action and mental illness, the idea of demons and demon-possession had already taken firm hold in religions and had become inextricably entwined with religious beliefs and the sacred writings they're based on. So people still held on to the superstitious idea of demons and demon possession even though science had already provided an answer for behavior of those said to be possessed.
So I believe that ignorance of the brain's functioning and malfunctioning led to the development of the idea of possession by spirits. And once this idea became a part of sacred religious beliefs and texts, loyalty to these said sacred religious beliefs preserved and perpetuated this false idea of demon possession long after science has presented a reasonable and logical explanation.